package org.mms.controller;

import org.mms.pojo.dto.TransportationDto;
import org.mms.pojo.entity.Transportation;
import org.mms.pojo.request.TransportationRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.TransportationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/transportations")
public class TransportationController {
    @Autowired
    private TransportationService transportationService;

    @GetMapping
    public ResponseMessage<List<TransportationDto>> list() {
        return ResponseMessage.success(transportationService.list()
                .stream()
                .map(Transportation::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<TransportationDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(transportationService.getById(id)
                .convert());
    }

    @PostMapping
    public ResponseMessage<TransportationDto> create(@RequestBody TransportationRequest request) {
        return ResponseMessage.success(transportationService.create(request)
                .convert());
    }

    @PutMapping
    public ResponseMessage<TransportationDto> update(@RequestBody TransportationRequest request) {
        return ResponseMessage.success(transportationService.update(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        transportationService.delete(id);
        return ResponseMessage.success(null);
    }
}